package com.hiwhitley.easy.day05;

/**
 * Created by hiwhitley on 2016/8/4.
 */
public class FindSolution {
    public boolean Find(int [][] array,int target) {
        if (array == null)
            return false;
        int rowLength = array.length;
        int columnLength = array[0].length;
        int x = rowLength -1;
        int y = 0;
        while (x >=0 && y <= columnLength - 1) {
            if (target < array[x][y]) {
                x--;
            } else if (target > array[x][y]) {
                y++;
            } else {
                return true;
            }
        }
        return false;
    }

    public boolean find2(int[][] array, int target) {

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                int low = 0;
                int high = array[i].length -1;
                while (low <= high) {
                    int mid = (low + high) / 2;
                    if (target > array[i][mid]) {
                        low = mid + 1;
                    } else if (target < array[i][mid]) {
                        high = mid - 1;
                    } else {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}
